package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserListReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.*;
import com.google.common.collect.Lists;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserService;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.User;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
　　* @description: 用户信息服务
　　* @author shenrm
　　* @date 2021/11/5 17:47
　　*/
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public List<UserInfo> getUserList(String userName) {
        if(StringUtils.isBlank(userName)) {
            return Collections.emptyList();
        }
        // 构造查询条件对象
        SelectUserListReqVO reqVO = new SelectUserListReqVO();
        reqVO.setUserName(userName);
        BaseResult<List<UserInfoResVO>>  listBaseResult  =  userFeignService.qryUserList(reqVO);
        List<UserInfoResVO> userInfoResVOS = listBaseResult.getData();
        if(CollectionUtils.isNotEmpty(userInfoResVOS)){
            List<UserInfo> userInfos = new ArrayList<>();
            for (UserInfoResVO userInfoResVO: userInfoResVOS) {
                UserInfo userInfo = new UserInfo();
                userInfo = new UserInfo();
                userInfo.setUserName(userInfoResVO.getUserName());
                userInfo.setUserCode(String.valueOf(userInfoResVO.getUserId()));
                userInfos.add(userInfo);
            }
            return userInfos;
        }
        return Collections.emptyList();
    }

    @Override
    public User getFullUserInfo(String userId) {
        if(StringUtils.isBlank(userId)) {
            return null;
        }
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setUserId(Long.parseLong(userId));
        BaseResult<RpcUserInfoVO> result;
        try {
            result = userFeignService.qryUserInfo(selectUserInfoReqVO);
        } catch (Exception ex) {
            log.error("用户组件无法连接到", ex);
            return null;
        }
        RpcUserInfoVO userInfoVo = result.getData();
        User user = null;
        if(Objects.nonNull(userInfoVo)){
            user = new User();
            // 获取用户信息
            user.setUserCode(String.valueOf(userInfoVo.getUserId()));
            user.setUserName(userInfoVo.getUserName());

            // 获取组织信息
            List<UserOrgResVO>  orgVoList = userInfoVo.getOrgList();
            if(CollectionUtils.isNotEmpty(orgVoList)){
                List<OrgInfo> orgInfos = new ArrayList<>();
                for(UserOrgResVO orgVo : orgVoList){
                    OrgInfo orgInfo = new OrgInfo();
                    orgInfo.setOrgCode(String.valueOf(orgVo.getOrgId()));
                    orgInfo.setOrgName(orgVo.getOrgName());
                    orgInfos.add(orgInfo);
                }
                user.setOrgInfos(orgInfos);
            }

            // 获取用户组信息
            List<UserGroupResVO>  userGroupResVOS = userInfoVo.getUserGroupList();
            if(CollectionUtils.isNotEmpty(userGroupResVOS)){
                List<GroupInfo> groupInfos = new ArrayList<>();
                for(UserGroupResVO groupResVO : userGroupResVOS){
                    GroupInfo groupInfo = new GroupInfo();
                    groupInfo.setGroupCode(String.valueOf(groupResVO.getGroupId()));
                    groupInfo.setGroupName(groupResVO.getGroupName());
                    groupInfos.add(groupInfo);
                }
                user.setGroupInfos(groupInfos);
            }

            // 获取岗位信息
            List<UserPositionResVO>  userPositionResVOS = userInfoVo.getPositionList();
            if(CollectionUtils.isNotEmpty(userPositionResVOS)){
                List<PositionInfo> positionInfos = new ArrayList<>();
                for(UserPositionResVO userPositionResVO : userPositionResVOS){
                    PositionInfo positionInfo = new PositionInfo();
                    positionInfo.setPositionCode(String.valueOf(userPositionResVO.getPositionId()));
                    positionInfo.setPositionName(userPositionResVO.getPositionName());
                    positionInfos.add(positionInfo);
                }
                user.setPositionInfos(positionInfos);
            }

            // 获取角色信息
            List<UserRoleResVO>  userRoleResVOS = userInfoVo.getRoleList();
            if(CollectionUtils.isNotEmpty(userRoleResVOS)){
                List<RoleInfo> roleInfos = new ArrayList<>();
                for(UserRoleResVO userRoleResVO : userRoleResVOS){
                    RoleInfo roleInfo = new RoleInfo();
                    roleInfo.setRoleCode(String.valueOf(userRoleResVO.getRoleId()));
                    roleInfo.setRoleName(userRoleResVO.getRoleName());
                    roleInfos.add(roleInfo);
                }
                user.setRoleInfos(roleInfos);
            }
        }
        return user;
    }

    @Override
    public UserInfo getUserInfoObj(String userId) {
        if(StringUtils.isBlank(userId)) {
            return null;
        }
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setUserId(Long.parseLong(userId));
        BaseResult<RpcUserInfoVO> result;
        try {
            result = userFeignService.qryUserInfo(selectUserInfoReqVO);
        } catch (Exception ex) {
            log.error("用户组件无法连接到", ex);
            return null;
        }
        RpcUserInfoVO userInfoVo = result.getData();
        if(Objects.nonNull(userInfoVo)){
            UserInfo userInfo = new UserInfo();
            userInfo.setUserCode(String.valueOf(userInfoVo.getUserId()));
            userInfo.setUserName(userInfoVo.getUserName());
            userInfo.setEmployeeCode(userInfoVo.getEmployeeCode());
            userInfo.setUserAccount(userInfoVo.getUserAccount());
            return userInfo;
        }
        return null;
    }

    @Override
    public User getSingleUser(String userId) {
        UserInfo userInfo = getUserInfoObj(userId);
        if (Objects.nonNull(userInfo)){
            User user = new User();
            user.setUserCode(userInfo.getUserCode());
            user.setUserName(userInfo.getUserName());
            return user;
        }
        return null;
    }

    @Override
    public List<UserInfo> getUserListByUserIdList(List<String> userIdList) {
        if(CollectionUtils.isEmpty(userIdList)) {
            return Collections.emptyList();
        }

        // 存储批量查询的用户信息列表
        List<UserInfo> userInfosList = new ArrayList<>();

        // 类型转换
        List<Long> userIdLongList = userIdList.stream().map(userId -> {
            return Long.parseLong(userId);
        }).collect(Collectors.toList());

        //按每50个一组分割,用户组件该接口做了限制,每次只能查询50个
        List<List<Long>> userIdParts = Lists.partition(userIdLongList, 50);

        // 按照userId进行分次批量查询
        userIdParts.stream().forEach(longList -> {
            // 构造查询条件对象
            SelectUserListReqVO reqVO = new SelectUserListReqVO();
            reqVO.setUserIdList(longList);
            BaseResult<List<UserInfoResVO>>  listBaseResult = userFeignService.qryUserList(reqVO);
            List<UserInfoResVO> userInfoResVOS = listBaseResult.getData();
            if(CollectionUtils.isNotEmpty(userInfoResVOS)){
                List<UserInfo> userInfos = new ArrayList<>();
                for (UserInfoResVO userInfoResVO: userInfoResVOS) {
                    UserInfo userInfo = new UserInfo();
                    userInfo.setUserName(userInfoResVO.getUserName());
                    userInfo.setUserCode(String.valueOf(userInfoResVO.getUserId()));
                    userInfo.setEmployeeCode(userInfoResVO.getEmployeeCode());
                    userInfos.add(userInfo);
                }
                userInfosList.addAll(userInfos);
            }
        });
        return userInfosList;
    }

    @Override
    public List<UserInfo> getUserListByEmployeeCodeList(List<String> employeeCodeList) {
        if(CollectionUtils.isEmpty(employeeCodeList)) {
            return Collections.emptyList();
        }
        // 构造查询条件对象
        SelectUserListReqVO reqVO = new SelectUserListReqVO();
        reqVO.setEmployeeCodeList(employeeCodeList);
        BaseResult<List<UserInfoResVO>>  listBaseResult = userFeignService.qryUserList(reqVO);
        List<UserInfoResVO> userInfoResVOS = listBaseResult.getData();
        if(CollectionUtils.isNotEmpty(userInfoResVOS)){
            List<UserInfo> userInfos = new ArrayList<>();
            for (UserInfoResVO userInfoResVO: userInfoResVOS) {
                UserInfo userInfo = new UserInfo();
                userInfo.setUserName(userInfoResVO.getUserName());
                userInfo.setUserCode(String.valueOf(userInfoResVO.getUserId()));
                userInfo.setEmployeeCode(userInfoResVO.getEmployeeCode());
                userInfos.add(userInfo);
            }
            return userInfos;
        }
        return Collections.emptyList();
    }

    @Override
    public UserInfo getUserInfoByUserAccount(String userAccount) {
        if(StringUtils.isBlank(userAccount)) {
            return null;
        }
        SelectUserInfoReqVO selectUserInfoReqVO = new SelectUserInfoReqVO();
        selectUserInfoReqVO.setUserAccount(userAccount);
        BaseResult<RpcUserInfoVO> result;
        try {
            result = userFeignService.qryUserInfo(selectUserInfoReqVO);
        } catch (Exception ex) {
            log.error("用户组件无法连接到", ex);
            return null;
        }
        RpcUserInfoVO userInfoVo = result.getData();
        if(Objects.nonNull(userInfoVo)){
            UserInfo userInfo = new UserInfo();
            userInfo.setUserCode(String.valueOf(userInfoVo.getUserId()));
            userInfo.setUserName(userInfoVo.getUserName());
            userInfo.setEmployeeCode(userInfoVo.getEmployeeCode());
            userInfo.setUserAccount(userInfoVo.getUserAccount());
            return userInfo;
        }
        return null;
    }

    @Override
    public String getEmployeeCodeById(String userId) {
        // 不为空,则根据人员编码查询获取用户信息
        if (StringUtils.isNotBlank(userId)) {
            UserInfo user = getUserInfoObj(userId);
            if (null != user) {
                return user.getEmployeeCode();
            }
        }
        return userId;
    }
}
